package com.qf.main.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.common.db.utils.CommonBeanutils;
import com.qf.main.content.common.qo.DiscoveryQo;
import com.qf.main.content.common.vo.DiscoveryVo;
import com.qf.main.content.entity.Discovery;
import com.qf.main.content.mapper.DiscoveryMapper;
import com.qf.main.content.service.DiscoveryService;
// import com.qf.main.login.utils.CommonBeanutils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author chengqiang
 * @Create 2024-01-22-21:08
 * @Description:
 */
@Service
public class DiscoveryServiceImpl implements DiscoveryService {

    @Resource
    private DiscoveryMapper discoveryMapper;

    @Override
    public IPage<DiscoveryVo> listDiscovery(Integer pageNum, Integer pageSize,String keywords) {
        Page<Discovery> page = new Page<Discovery>(pageNum, pageSize);
        QueryWrapper<Discovery> qw = new QueryWrapper<Discovery>();
        qw.like(Discovery.COL_DISCOVERY_TITLE,keywords)
                .or()
                .like(Discovery.COL_USERNAME,keywords)
                .or()
                .like(Discovery.COL_SOURCE,keywords);
        Page<Discovery> discoveryPage = discoveryMapper.selectPage(page, qw);

        IPage<DiscoveryVo> discoveryVoIPage = CommonBeanutils.convertPageInfo(discoveryPage, new Page<DiscoveryVo>(), DiscoveryVo::new);
        return discoveryVoIPage;
    }

    @Override
    public Integer saveDiscovery(DiscoveryQo discoveryQo) {
        Discovery discovery = CommonBeanutils.convertTo(discoveryQo, Discovery::new);
        return discoveryMapper.insert(discovery);
    }

    @Override
    public Integer deleteDiscovery(Integer id) {
        return discoveryMapper.deleteById(id);
    }

    @Override
    public Integer modify(DiscoveryQo discoveryQo) {
        Discovery discovery = CommonBeanutils.convertTo(discoveryQo, Discovery::new);
        return discoveryMapper.updateById(discovery);
    }
}
